package org.jground.zk.zulX;

import org.jground.zk.util.ZKUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Cell;
import org.zkoss.zul.Column;
import org.zkoss.zul.Columns;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;

public class GridX extends Grid {

    private Columns cols;
    private Rows rows;
    private Row currentRow;

    public enum InitTypes {
        HFLEX
    };

    public enum GridType {
        LABEL_CONTROL
    };

    public GridX(Column... columns) {
        for (Column col : columns) {
            col.setParent(getColumnsX());
        }
    }

    public GridX(Component parent, int nrOfColumns) {
        this(nrOfColumns);
        setParent(parent);
    }

    public GridX(int nrOfColumns) {
        for (int i = 0; i < nrOfColumns; i++) {
            Column col = new Column();
            col.setParent(getColumnsX());
        }
    }

    public GridX(String... sizes) {
        for (int i = 0; i < sizes.length; i++) {
            Column col = new Column();
            col.setWidth(sizes[i]);
            col.setParent(getColumnsX());
        }
    }

    public GridX(InitTypes type, String... sizes) {
        for (int i = 0; i < sizes.length; i++) {
            Column col = new Column();
            col.setHflex(sizes[i]);
            col.setParent(getColumnsX());
        }
    }

    public Columns getColumnsX() {
        if (cols == null) {
            cols = new Columns();
            cols.setParent(this);
        }
        return cols;
    }

    public Rows getRowsX() {
        if (rows == null) {
            rows = new Rows();
            rows.setParent(this);
        }
        return rows;
    }

    public void addRow(Row... rows) {
        for (Row row : rows) {
            getRowsX().setParent(row);
        }
    }

    public void addAsRow(Component... components) {
        Row row = new Row();
        row.setParent(getRowsX());
        // row.setStyle("white-space: nowrap");
        row.setAlign("left");
        for (Component component : components) {
            component.setParent(row);
        }
    }

    public void addCells(Component... components) {
        for (Component component : components) {
            component.setParent(currentRow);
        }
    }

    public void addCells(Cell... cells) {
        Row row = new Row();
        row.setParent(getRowsX());
        for (Component cell : cells) {
            cell.setParent(currentRow);
        }
    }

    public void newRow(boolean nowrap) {
        currentRow = new Row();
        currentRow.setParent(getRowsX());
        if (nowrap) {
            currentRow.setStyle("white-space: nowrap");
        }
    }

    public void newRow(int colspan, Component component, boolean nowrap) {
        currentRow = new Row();
        currentRow.setParent(getRowsX());
        // currentRow.
        Cell cell = new Cell();
        cell.setParent(currentRow);
        cell.setColspan(colspan);
        component.setParent(cell);

        if (nowrap) {
            currentRow.setStyle("white-space: nowrap");
        }
    }

    public void removeAllRows() {
        if (rows != null) {
            ZKUtils.removeAllChildren(rows);
        }
    }
}
